//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    city:'guangzhou',
     citys:[{
          city:'guangzhou',
          name:'广州'
    },{
          city:'shenzhen',
          name:'深圳'
    },{
          city:'beijing',
          name:'北京'
    },{
          city:'shanghai',
          name:'上海'
    }],
    input:{
      showTip:false,
    },
    inputValue:'',
    likes:[],
    choices:[],
    oldInputValue:''
  },
   changeCity:function(event){
   var value = event.target.dataset.value;
   this.setData({
     city:value,
     inputValue:'',
     choices:[]
   });
   
  },
  //事件处理函数
  getDataFromNetwork:function(url,data,callback){
    wx.request({
      url: url,
      data:data,
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
       callback(res);
      },
      fail:function(res){
        console.log(res);
      }
    });
  },
  
  bindInput: function(event) {
   console.log(event);
   var value = event.detail.value;
    var url="https://www.tongquxiang.com/busapi/stoplike";
    var that = this;
    var data ={
      city:this.data.city,
      stoplike:value,
    };
    this.setData({
      inputValue:value
    });
    this.getDataFromNetwork(url,data,function(res){
      console.log(res);
      var data = res.data;
      if(data.length == 0)return;
      that.setData({
        likes:data,
        input:{
        showTip:true
        }
      });
    });
  
  },
  
  changeInput: function(event) {
    var value = event.target.dataset.value;
   this.setData({
     input:{
       showTip:false,
     },
     inputValue:value
   })
  },
 queryBus:function(){
    var url = "https://www.tongquxiang.com/busapi/stop";
    var that = this;
    if(this.data.inputValue == '') {
      this.showModal('输入站点');
      return;
    }
    var data={
      stop:that.data.inputValue,
      city:that.data.city
    }
    this.showToast();
    this.getDataFromNetwork(url,data,function(res){
      that.hideToast();
      var choices = res.data;
     console.log(choices);
      that.setData({
        oldInputValue:that.data.inputValue,
        choices:choices
      });
    });
  },
 
 redirectToLinePage:function(event){
    var value = event.target.dataset.value;
    var that = this;
    var line = {
      city:that.data.city,
      line:value
    }
    try {
        wx.setStorageSync('line', JSON.stringify(line));
    } catch (e) {    
    }
    wx.switchTab({url: '/pages/line/index'});
  },
  showToast:function(){
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });
  },
  hideToast:function(){
    wx.hideToast();
  },
  showModal:function(msg){
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel:false,
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    
  },
  onShow:function(){
    var stop = wx.getStorageSync('stop');
    if(stop==undefined||stop==null||stop=='')return;
     wx.removeStorageSync("stop");
    var data = JSON.parse(stop);
    this.setData({
      inputValue:data.stop,
      city:data.city
    });
    this.queryBus();
  }
})
